package com.mftour.moudles.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.mftour.common.Constant;
import com.mftour.moudles.service.AccountBookService;
import com.mftour.moudles.vo.AjaxResponse;

@Deprecated
@RestController
public class FileUploadController {
	private static final Logger logger = LoggerFactory.getLogger(FileUploadController.class);
	
	@Autowired
	private AccountBookService accountBookSer;

	/**
	 * Upload single file using Spring Controller
	 */
	@RequestMapping(value = "/ajax/uploadFile", method = RequestMethod.POST)
	public AjaxResponse<Object> uploadFileHandler(@RequestParam MultipartFile file,Integer id,String name) throws IOException {
		AjaxResponse<Object> ajaxResponse = new AjaxResponse<Object>(Constant.RS_CODE_ERROR, "文件上传失败");

		if (!file.isEmpty()) {
			InputStream in = null;
			OutputStream out = null;

			try {
				File dir = new File("tmpFiles");
				if (!dir.exists()) {
					dir.mkdirs();
				}
				
				File serverFile = new File(dir.getAbsolutePath() + File.separator + System.currentTimeMillis());
				in = file.getInputStream();
				out = new FileOutputStream(serverFile);
				byte[] b = new byte[1024];
				int len = 0;
				while ((len = in.read(b)) > 0) {
					out.write(b, 0, len);
				}
				out.close();
				in.close();
				logger.info("Server File Location=" + serverFile.getAbsolutePath());

				if(accountBookSer.parseAccountBookExcel(serverFile)){
					ajaxResponse.setCode(Constant.RS_CODE_SUCCESS);
					ajaxResponse.setMsg("上传成功");
				}				
				return ajaxResponse;
			} catch (Exception e) {
				e.printStackTrace();
				return ajaxResponse;
			} finally {
				if (out != null) {
					out.close();
					out = null;
				}

				if (in != null) {
					in.close();
					in = null;
				}
			}
		} else {
			ajaxResponse.setMsg("文件为空！");
			return ajaxResponse;
		}
	}

	/**
	 * Upload multiple file using Spring Controller
	 */
	@RequestMapping(value = "/ajax/uploadMultipleFile", method = RequestMethod.POST, produces = "application/json;charset=utf8")
	@ResponseBody
	public AjaxResponse<Object> uploadMultipleFileHandler(@RequestParam("file") MultipartFile[] files) throws IOException {

		AjaxResponse<Object> ajaxResponse = new AjaxResponse<Object>(Constant.RS_CODE_ERROR, "文件上传失败");
		ArrayList<Integer> arr = new ArrayList<>();

		for (int i = 0; i < files.length; i++) {
			MultipartFile file = files[i];

			if (!file.isEmpty()) {
				InputStream in = null;
				OutputStream out = null;

				try {
					File dir = new File("tmpFiles");
					if (!dir.exists())
						dir.mkdirs();
					File serverFile = new File(dir.getAbsolutePath() + File.separator + file.getOriginalFilename());
					in = file.getInputStream();
					out = new FileOutputStream(serverFile);
					byte[] b = new byte[1024];
					int len = 0;
					while ((len = in.read(b)) > 0) {
						out.write(b, 0, len);
					}
					out.close();
					in.close();
					logger.info("Server File Location=" + serverFile.getAbsolutePath());

				} catch (Exception e) {
					e.printStackTrace();
					arr.add(i);
				} finally {
					if (out != null) {
						out.close();
						out = null;
					}

					if (in != null) {
						in.close();
						in = null;
					}
				}
			} else {
				arr.add(i);
			}

		}

		if (arr.size() > 0) {

		} else {
			ajaxResponse.setCode(Constant.RS_CODE_SUCCESS);
			ajaxResponse.setMsg("所有文件上传成功");
		}
		return ajaxResponse;
	}
}